# ----------------------------------------------------------------------
#  Paper: Party Competition and Cooperation Shape Affective Polarization
#  Last updated: February 2021
#  Purpose: Clean data for survey experiment (Study II)
#  Outputs: Survey_analysis.csv
#  Machine: Chagai's macbook pro
# ----------------------------------------------------------------------

# ----------------------------------------------------------------------
# load all relevant themes and packages
# ----------------------------------------------------------------------
rm(list=ls())
library("tidyverse")
library("ggplot2")
library("stargazer")
library("xtable")
library("texreg")
library("reshape2")
library("wesanderson")
library("RItools")
library("estimatr")
library("fastDummies")
library("ggpubr")
library("estimatr")
library("mediation")
library("car")
library("table1")
library("standardize")
library("tidyverse")
library("readstata13")
library("naniar")
library("lfe")

# ----------------------------------------------------------------------
# Read data
# ----------------------------------------------------------------------

### read data
data <- read_csv("Raw Data/Survey/data_oct21.csv")

# clean first three rows which are qualtrics meta-data
data <- data[3:nrow(data),]



# re-write data as a clean version without the first two rows
write_csv(data, "Raw Data/Survey/clean_oct21.csv")


# reload the data as clean version
data <- read_csv("Raw Data/Survey/clean_oct21.csv")


# number of obs before cleaning
nrow(data) # 2633

# ----------------------------------------------------------------------
# Define and recode main treatment groups
# ----------------------------------------------------------------------

# Treatment groups include:
# 1. Unity gov, no unity gov
# 2. Netanyahu or gantz next pm

data <- data %>%
  mutate(.,
         pm_eng = case_when(
           grepl("גנץ", pm)~"Gantz",
           grepl("נתניהו", pm)~"Netanyahu"
         ),
         unity = ifelse(treat == "Unity Government",
                        1,
                        0),
         el_narrow = ifelse(treat == "Narrow Government",
                            1,
                            0))

# filter out no consent and respondents that did not pass quota stage
data <- data %>% 
  filter(.,
         consent == 1,
         !is.na(pm_eng))

# number of obs after ommitting no consent
nrow(data) # 1524

# ----------------------------------------------------------------------
# Code main covariates
# ----------------------------------------------------------------------


data <- data %>%
  mutate(.,
         party_vote = case_when(
           vote == 1 ~ "Likud",
           vote == 2 ~ "Blue-White",
           vote == 3 ~ "Israel Betenu",
           vote == 4 ~ "Labor",
           vote == 5 ~ "Yamina",
           vote == 6 ~ "Joint List",
           vote == 7 ~ "Shas",
           vote == 8 ~ "UTJ",
           vote == 9 ~ "Democratic Union",
           vote == 10 ~ "Otzma",
           vote == 11 ~ "Other",
           vote == 12 ~ "No Vote"
         ),
         rw_ideology = case_when(
           left_right_1 < 4 ~ 1,
           left_right_1 > 4 ~ 0),# Note that this measure omits over 300 people who are center
         pm_new = case_when(
           pm_eng == "Gantz" ~ 0,
           pm_eng == "Netanyahu" ~ 1)) 

# Change variable names
main_data <- data %>%
  rename(.,
         therm_left = therm_ppl_1,
         therm_right = therm_ppl_2,
         therm_uo = therm_ppl_3,
         therm_arabs = therm_ppl_4,
         therm_likud = therm_party_1,
         therm_bw = therm_party_2,
         therm_isrlb = therm_party_3, 
         therm_lbr = therm_party_4, 
         therm_utj = therm_party_5,
         therm_dem = therm_party_6,
         therm_shas = therm_party_7,
         therm_joint = therm_party_8,
         therm_yamina = therm_party_9,
         sdis_right = social_d_1,
         sdis_left = social_d_2,
         sdis_uo = social_d_3,
         sdis_arb = social_d_4,
         sdis_likud = social_d_5,
         sdis_bw = social_d_6,
         sdis_isrlb = social_d_7,
         sdis_lbr = social_d_8, 
         sdis_utj = social_d_9,
         sdis_dem = social_d_10,
         sdis_shas = social_d_11,
         sdis_joint = social_d_12,
         sdis_yamina = social_d_13,
         prt_id_likud = party_ideology_1,
         prt_id_bw = party_ideology_2)

# ----------------------------------------------------------------------
# Create main outcomes
# ----------------------------------------------------------------------

# Reverse Social distance measure so large number means closer relation to out-party (like thermometor)

main_data <- main_data %>% 
  mutate(.,
         sdis_right = 8 - as.numeric(sdis_right),
         sdis_left = 8 - as.numeric(sdis_left),
         sdis_uo = 8 - as.numeric(sdis_uo),
         sdis_arb = 8 - as.numeric(sdis_arb),
         sdis_likud = 8 - as.numeric(sdis_likud),
         sdis_bw = 8 - as.numeric(sdis_bw),
         sdis_isrlb = 8 - as.numeric(sdis_isrlb),
         sdis_lbr = 8 - as.numeric(sdis_lbr),
         sdis_utj = 8 - as.numeric(sdis_utj),
         sdis_dem = 8 - as.numeric(sdis_dem),
         sdis_shas = 8 - as.numeric(sdis_shas),
         sdis_joint = 8 - as.numeric(sdis_joint),
         sdis_yamina = 8 - as.numeric(sdis_yamina))


### Create main outcome measures
# Two party polarization therm/SD
# General polarization therm/SD (Including center voters as left)
# General polarization therm/SD (Not including center voters as left)
# Polarization based on self reported vote rather than therm therm/sd

main_data <- main_data %>% rowwise() %>%
  mutate(.,
         two_prty_therm_polar = case_when(
           party_vote == "Likud" ~ as.numeric(therm_likud) - as.numeric(therm_bw),
           party_vote == "Blue-White" ~ as.numeric(therm_bw) - as.numeric(therm_likud)
         ), 
         two_prty_socd_polar = case_when(
           party_vote == "Likud" ~ as.numeric(sdis_likud) - as.numeric(sdis_bw),
           party_vote == "Blue-White" ~ as.numeric(sdis_bw) - as.numeric(sdis_likud)
         ),
         general_therm_polar = case_when(
           left_right_1 < 4 ~ as.numeric(therm_right) - as.numeric(therm_left),
           left_right_1 > 3 ~ as.numeric(therm_left) - as.numeric(therm_right)
         ),
         general_therm = case_when(
           left_right_1 < 4 ~  as.numeric(therm_left),
           left_right_1 > 3 ~ as.numeric(therm_right)
         ),
         general_socd_polar = case_when(
           left_right_1 < 4 ~ as.numeric(sdis_right) - as.numeric(sdis_left),
           left_right_1 > 3 ~ as.numeric(sdis_left) - as.numeric(sdis_right)
         ),
         general_socd = case_when(
           left_right_1 < 4 ~ as.numeric(sdis_left),
           left_right_1 > 3 ~ as.numeric(sdis_right)
         ),
         general_therm_polar_nc = case_when(
           left_right_1 < 4 ~ as.numeric(therm_right) - as.numeric(therm_left),
           left_right_1 > 4 ~ as.numeric(therm_left) - as.numeric(therm_right)
         ),
         general_therm_nc = case_when(
           left_right_1 < 4 ~ as.numeric(therm_left),
           left_right_1 > 4 ~ as.numeric(therm_right)
         ),
         general_therm_nc_in = case_when(
           left_right_1 < 4 ~ as.numeric(therm_right),
           left_right_1 > 4 ~ as.numeric(therm_left)
         ),
         general_socd_polar_nc = case_when(
           left_right_1 < 4 ~ as.numeric(sdis_right) - as.numeric(sdis_left),
           left_right_1 > 4 ~ as.numeric(sdis_left) - as.numeric(sdis_right)
         ),
         general_socd_nc = case_when(
           left_right_1 < 4 ~ as.numeric(sdis_left),
           left_right_1 > 4 ~  as.numeric(sdis_right)
         ),
         general_socd_nc_in = case_when(
           left_right_1 < 4 ~ as.numeric(sdis_right),
           left_right_1 > 4 ~  as.numeric(sdis_left)
         ),
         prt_therm_polar = case_when(
           party_vote == "Likud" ~ as.numeric(therm_right) - as.numeric(therm_left),
           party_vote == "Israel Betenu" ~ as.numeric(therm_right) - as.numeric(therm_left),
           party_vote == "Yamina" ~ as.numeric(therm_right) - as.numeric(therm_left),
           party_vote == "Otzma" ~ as.numeric(therm_right) - as.numeric(therm_left),
           party_vote == "UTJ" ~ as.numeric(therm_right) - as.numeric(therm_left),
           party_vote == "Shas" ~ as.numeric(therm_right) - as.numeric(therm_left),
           party_vote == "Blue-White" ~ as.numeric(therm_left) - as.numeric(therm_right),
           party_vote == "Democratic Union" ~ as.numeric(therm_left) - as.numeric(therm_right),
           party_vote == "Joint List" ~ as.numeric(therm_left) - as.numeric(therm_right),
           party_vote == "Labor" ~ as.numeric(therm_left) - as.numeric(therm_right)
         ),
         prt_therm = case_when(
           party_vote == "Likud" ~ as.numeric(therm_left),
           party_vote == "Israel Betenu" ~  as.numeric(therm_left),
           party_vote == "Yamina" ~  as.numeric(therm_left),
           party_vote == "Otzma" ~  as.numeric(therm_left),
           party_vote == "UTJ" ~  as.numeric(therm_left),
           party_vote == "Shas" ~  as.numeric(therm_left),
           party_vote == "Blue-White" ~ as.numeric(therm_right),
           party_vote == "Democratic Union" ~  as.numeric(therm_right),
           party_vote == "Joint List" ~  as.numeric(therm_right),
           party_vote == "Labor" ~  as.numeric(therm_right)
         ),
         prt_sd_polar = case_when(
           party_vote == "Likud" ~ as.numeric(sdis_right) - as.numeric(sdis_left),
           party_vote == "Israel Betenu" ~ as.numeric(sdis_right) - as.numeric(sdis_left),
           party_vote == "Yamina" ~ as.numeric(sdis_right) - as.numeric(sdis_left),
           party_vote == "Otzma" ~ as.numeric(sdis_right) - as.numeric(sdis_left),
           party_vote == "UTJ" ~ as.numeric(sdis_right) - as.numeric(sdis_left),
           party_vote == "Shas" ~ as.numeric(sdis_right) - as.numeric(sdis_left),
           party_vote == "Blue-White" ~ as.numeric(sdis_left) - as.numeric(sdis_right),
           party_vote == "Democratic Union" ~  as.numeric(sdis_left) - as.numeric(sdis_right),
           party_vote == "Joint List" ~  as.numeric(sdis_left) - as.numeric(sdis_right),
           party_vote == "Labor" ~  as.numeric(sdis_left) - as.numeric(sdis_right)
         ),
         prt_sd = case_when(
           party_vote == "Likud" ~  as.numeric(sdis_left),
           party_vote == "Israel Betenu" ~  as.numeric(sdis_left),
           party_vote == "Yamina" ~ as.numeric(sdis_left),
           party_vote == "Otzma" ~  as.numeric(sdis_left),
           party_vote == "UTJ" ~  as.numeric(sdis_left),
           party_vote == "Shas" ~  as.numeric(sdis_left),
           party_vote == "Blue-White" ~ as.numeric(sdis_right),
           party_vote == "Democratic Union" ~   as.numeric(sdis_right),
           party_vote == "Joint List" ~   as.numeric(sdis_right),
           party_vote == "Labor" ~   as.numeric(sdis_right)
         ),
         left_sd = mean(c(as.numeric(sdis_lbr),as.numeric(sdis_dem)), na.rm = T),
         left_therm = mean(c(as.numeric(therm_lbr), as.numeric(therm_dem))),
         right_sd = mean(c(as.numeric(sdis_isrlb),as.numeric(sdis_likud), 
                           as.numeric(sdis_yamina)), na.rm = T),
         right_therm = mean(c(as.numeric(therm_likud), as.numeric(therm_isrlb), 
                              as.numeric(therm_yamina))),
         sd_ines_polar = 
           case_when(
             left_right_1 < 4 ~ as.numeric(right_sd) - as.numeric(left_sd),
             left_right_1 >4 ~ as.numeric(left_sd) - as.numeric(right_sd)
           ),
         therm_ines_polar = 
           case_when(
             left_right_1 < 4 ~ as.numeric(right_therm) - as.numeric(left_therm),
             left_right_1 >4 ~ as.numeric(left_therm) - as.numeric(right_therm)
           ),
         main_prty_therm_polar = case_when(
           left_right_1 < 4 ~ as.numeric(therm_likud) - as.numeric(therm_bw),
           left_right_1 >4 ~ as.numeric(therm_bw) - as.numeric(therm_likud)
         ), 
         main_prty_socd_polar = case_when(
           left_right_1 < 4 ~ as.numeric(sdis_likud) - as.numeric(sdis_bw),
           left_right_1 >4 ~ as.numeric(sdis_bw) - as.numeric(sdis_likud)
         ),
         ext_prty_therm_polar = case_when(
           left_right_1 < 4 ~ as.numeric(therm_yamina) - as.numeric(therm_dem),
           left_right_1 > 4 ~ as.numeric(therm_dem) - as.numeric(therm_yamina)
         ), 
         ext_prty_socd_polar = case_when(
           left_right_1 < 4 ~ as.numeric(sdis_yamina) - as.numeric(sdis_dem),
           left_right_1 >4 ~ as.numeric(sdis_dem) - as.numeric(sdis_yamina)
         ))

#Add attrition variables

main_data <- main_data %>% 
  mutate(.,
         attrition_socd = ifelse(general_socd_polar_nc == "NA", 1, 0),
         attrition_therm = ifelse(general_therm_polar_nc == "NA", 1, 0))

main_data$attrition_socd[is.na(main_data$attrition_socd)]<- 1
main_data$attrition_therm[is.na(main_data$attrition_therm)]<- 1

# ----------------------------------------------------------------------
# Save data
# ----------------------------------------------------------------------

write_csv(main_data, "Data for analysis/Survey_analysis.csv")
